package cn.ylyhappy.netdemo

import androidx.collection.SimpleArrayMap
import cn.ylyhappy.netdemo.config.CniaoInterceptor
import cn.ylyhappy.netdemo.config.KtHttpLogInterceptor
import cn.ylyhappy.netdemo.config.LocalCookie
import cn.ylyhappy.netdemo.config.RetryInterceptor
import okhttp3.Cache
import okhttp3.Call
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import java.io.File
import java.util.concurrent.TimeUnit

class KtRetrofit {
    companion object {
        private const val TAG = "KtRetrofit"

        @Volatile
        private var api: OkHttpApi? = null
//        fun getInstance(): OkHttpApi {
//            return api ?: synchronized(OkHttpApi::class.java) { KtRetrofit().also { api = it } }
//        }
    }

    private val mBaseUrl = "http://ylyhappy.cn:48080/"

    private val maxRetry = 0

    private val callMap = SimpleArrayMap<Any, Call>()

    private val mOkClient = OkHttpClient.Builder()
        .callTimeout(10, TimeUnit.SECONDS)
        .connectTimeout(10, TimeUnit.SECONDS)
        .readTimeout(10, TimeUnit.SECONDS)
        .writeTimeout(10, TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
        .followRedirects(true)
        .cache(Cache(File("sdcard/cache", "okhttp"), 1024))
//        .cookieJar(CookieJar.NO_COOKIES)
        .cookieJar(LocalCookie())
        .addNetworkInterceptor(CniaoInterceptor())
        .addNetworkInterceptor(KtHttpLogInterceptor().apply {
            mLogTag = "YLY"
            mLogLevel = KtHttpLogInterceptor.LogLevel.BODY
            mColorLevel = KtHttpLogInterceptor.ColorLevel.DEBUG
        })
        .addNetworkInterceptor(RetryInterceptor(maxRetry))
        .build()
    private val retrofitBuilder = Retrofit.Builder()
//        .addConverterFactory(GsonC)

    /**
     * 初始化配置，[baseUrl] 项目接口的基类url，以/结尾
     */
//    fun initConfig(baseUrl: String, okClient: OkHttpClient = mOkClient): KtRetrofit {
//
//    }

    
}
